package dns

import (
	"fmt"
	Dns "github.com/miekg/dns"
	"time"
)

func DnsRequest(dnsUrl string, domain string) (res string) {
	c := Dns.Client{
		Timeout: 5 * time.Second,
	}
	m := Dns.Msg{}
	m.SetQuestion(domain, Dns.TypeA)
	r, _, err := c.Exchange(&m, dnsUrl)
	if err != nil {
		fmt.Println(domain," dns error ",err)
		return
	}
	var a []string
	var cname []string
	for _, ans := range r.Answer {
		record, isType := ans.(*Dns.A)
		if isType {
			a = append(a, record.A.String())
		}
		cnames, isType := ans.(*Dns.CNAME)
		if isType {
			cname = append(cname, cnames.Target)
		}
	}
	cname = append(cname, a...)
	for _, v := range cname {
		if res != "" {
			v = "|" + v
		}
		res = res + v
	}
	return
}
